package edu.nuc.event.controller;


import edu.nuc.event.pojo.Place;
import edu.nuc.event.service.PlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class PlaceController {

    @Autowired
    private PlaceService placeService;

    @GetMapping("/toselect-place")
    public String toselectplace(Model model) {
        List<Place> placeList = placeService.findAllPlaces();

        model.addAttribute("placeList", placeList);

        int account = placeService.findAllPlaceAccount();

        model.addAttribute("account", account);
        return "toselect-place";
    }

    @GetMapping("/place-operation")
    public String placeoperation(Model model) {
        List<Place> placeList = placeService.findAllPlaces();

        model.addAttribute("placeList", placeList);

        int account = placeService.findAllPlaceAccount();

        model.addAttribute("account", account);

        return "place-operation";
    }

    @GetMapping("/todelect-palce")
    public String todelectpalce(@RequestParam("id") int id, Model model) {

        int i = placeService.delectplace(id);

        return "redirect:/place-operation";
    }

    @GetMapping("/toadd-place")
    public String toaddplace(Model model) {
        return "/toadd-place";
    }

    @PostMapping("/doadd-place")
    public String doaddplace(Place place) {

        int i = placeService.addplace(place);

        return "redirect:/place-operation";
    }

    @GetMapping("/toupdate-palce")
    public String toupdatepalce(@RequestParam("id") int id, Model model) {
        Place place = placeService.selectplacebyid(id);

        model.addAttribute("place", place);
        return "toupdate-place";
    }

    @PostMapping("/doupdate-place")
    public String doupdateplace(Place place) {

        int i = placeService.updateplace(place);

        return "redirect:/place-operation";
    }
}
